<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加BANNER</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="bannerStore.loading.list || bannerStore.loading.update"
        :data="bannerStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="名称" prop="name" />
        <el-table-column label="图片">
          <template #default="{ row }">
            <el-image
              :src="row.image_url"
              :preview-src-list="[row.image_url]"
              :preview-teleported="true"
              style="width: 50px; height: 50px"
            />
          </template>
        </el-table-column>
        <el-table-column label="跳转地址">
          <template #default="{ row }">
            <el-link type="primary" :href="row.skip_path">{{ row.skip_path }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="是否有效" prop="is_active">
          <template #default="{ row }">
            <el-tag :type="row.is_active ? 'success' : 'danger'">
              {{ row.is_active ? "有效" : "无效" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="bannerStore.totalCount"
        @change="bannerStore.fetchBannerList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="bannerStore.fetchBannerList(params)"
    />
  </div>
</template>

<script setup lang="ts">
import { BannerListParams, BannerType } from "@/api/system/banner";
import { useBannerStoreHook } from "@/store/modules/system/banner";
import Form, { type FormType } from "./components/form.vue";

const bannerStore = useBannerStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<BannerType>({
  name: "",
  image_url: "",
  is_active: true,
  skip_path: "",
  id: 0,
  created_at: "",
  updated_at: "",
});

const params = reactive<BannerListParams>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: BannerType) {
  await bannerStore.fetchDeleteBanner(row.id);
  await bannerStore.fetchBannerList(params);
}

function handleToEdit(row: BannerType) {
  formType.value = "edit";
  currentForm.value = row;
  visible.value = true;
}

onMounted(() => {
  bannerStore.fetchBannerList(params);
});
</script>

<style scoped lang="scss"></style>
